package org.example.hypersonaltools.manager;

import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import org.example.hypersonaltools.scene.FXScene;
import org.example.hypersonaltools.scene.PrimaryScene;

import java.util.HashMap;
import java.util.Map;

public class SceneManager {

    private static SceneManager instance;
    private Map<String, FXScene> scenes;
    private Stage stage;

    private SceneManager() {
        scenes = new HashMap<>();
    }

    public static SceneManager getInstance() {
        if (instance == null) {
            instance = new SceneManager();
        }
        return instance;
    }

    public void setStage(Stage stage) {
        this.stage = stage;
    }

    public void addScene(String name, FXScene scene) {
        scenes.put(name, scene);
    }

    public void switchScene(String name) {
        if (scenes.containsKey(name)) {
            FXScene currentScene = scenes.get(name);
            currentScene.onEnter();
            if (stage.getScene() != null) {
                stage.getScene().getStylesheets().clear();
            }
            stage.setScene(currentScene.getScene());
            stage.show();
        } else {
            System.err.println("Scene not found: " + name);
        }
    }

    public void switchChildScene(String HostScene,String sceneName) {
        if (scenes.containsKey(sceneName)) {
            FXScene fxScene = scenes.get(sceneName);
            Pane newContent = (Pane) fxScene.getScene().getRoot();
            PrimaryScene primaryScene = (PrimaryScene) scenes.get(HostScene);
            if (primaryScene != null) {
                primaryScene.getRoot().setCenter(newContent);
            }
        }
    }

    public void removeScene(String name) {
        if (scenes.containsKey(name)) {
            FXScene scene = scenes.get(name);
            scene.onExit();
            scenes.remove(name);
        } else {
            System.err.println("Scene not found: " + name);
        }
    }
}
